<?php
namespace app\mouth\model;
use think\Db;
use app\mouth\common\Encapsulation;
class ShopModel extends Encapsulation{

    public function score(){
        return $this->all('store','','','');
    }

    public function remindadd($openid){
        $time = strtotime(date ( 'Y-m-d' , strtotime ( '+1 month' )));
        $add['Insurance'] = Db::name('vehicle_user_account_maintain')->where('openid',$openid)->where('state',2)->whereTime('time', 'between', [time(), $time])->count();
        $add['Insurance'] ? $this->up('vehicle_user_account_maintain',['openid'=>$openid],['state'=>1]) : '';
        $add['maintain'] = Db::name('vehicle_user_account_insurance')->where('openid',$openid)->where('state',2)->whereTime('time', 'between', [time(), $time])->count('id');
        $add['maintain'] ? $this->up('vehicle_user_account_insurance',['openid'=>$openid],['state'=>1]) : '';
        $add['yearly_inspection'] = Db::name('vehicle_user_account_yearly_inspection')->where('openid',$openid)->where('state',2)->whereTime('time', 'between', [time(), $time])->count();
        $add['yearly_inspection'] ? $this->up('vehicle_user_account_yearly_inspection',['openid'=>$openid],['state'=>1]) : '';
        return $add;
    }
        public function index($openid){
                $value['shop']      = Db::name('store')->order('ma_id desc')->select();//所有店铺
                $value['vehicle']   = Db::name('vehicle_user')->where('openid',$openid)->where('default',1)->find();//客户车型
                $value['chart']     = Db::name('advert')->where('area',1)->select();//轮播图
//                $value['volume']    =Db::name('vehicle_user')->where('openid',$openid)->select();//车型
//            foreach ($value['shop'] as $k){
//                    $add[] = $k['ma_id'];
//            }
//
//            echo '<pre>';
//            var_dump($add);die;
//            echo 555;exit();
            return $value;
        }
        public static function allcars($openid){
            return Db::name('vehicle_user')->where('openid',$openid)->select();
        }
        public static function vehicleDe($openid,$id){
            return Db::name('vehicle_user')->where('openid',$openid)->where('id',$id)->delete();
        }
        public static function Silence($openid,$id){
            $data = Db::name('vehicle_user')->where('openid',$openid)->where('id',$id)->find();
            if ($data){
                Db::name('vehicle_user')->where('openid',$openid)->update(['default'=>2]);
                return Db::name('vehicle_user')->where('openid',$openid)->where('id',$id)->update(['default'=>1]);
            }
            return null;
        }
        public function shopDetails($ma_id){
            $ma_id = Db::table('yc_store')->where('ma_id',$ma_id)->find();//商铺详细信息
            $data['volume']         = Db::table('yc_car_volume_store')->where('st_id',$ma_id['ma_id'])->select();//商铺 卷
            $data['comment']        = Db::table('yc_order_comment')->where('ma_id',$ma_id['ma_id'])->select();//所有评论
            $data['number']         =Db::name('order')->where('ma_id',$ma_id['ma_id'])->where('or_status',2)->count();//订单
            $data['shop'] = $ma_id;
            return $data;
        }
        public static function reviews($openid,$portrait,$nickname,$user_id,$ma_id,$comment=''){
            if ($comment == ''){
                return '请输入内容';
            }
            $oreder = Db::name('order')->where('or_openid',$openid)->where('or_status',3)->where('ma_id',$ma_id)->find();
            if(empty($oreder)){
                return '购买后才能评论';
            }
            if($oreder['evaluate'] == 2){
                return '请勿重复评价';
            }
            $date['openid'] = $openid;
            $date['picture'] = $portrait;
            $date['nickname'] = $nickname;
            $date['ma_id'] = $ma_id;
            $date['u_id'] = $user_id['id'];
            $date['content'] = $comment;
            $date['time'] = time();
            Db::name('order_comment')->insert($date);
            return true;
        }
        public static function cehicleCategory($category){
            $where['vs_type'] = '';
            switch ($category) {
                case 4:
                    $where['vs_type'] = 4;
                    break;
                case 1:
                    $where['vs_type'] = 1;
                    break;
                case 3:
                    $where['vs_type'] = 3;
                    break;
            }
            $volume = Db::name('car_volume_store')->where($where)->select();
            return $volume;
        }

        public static function brand($b_id='',$g_id='',$s_id=''){
            if ($s_id){
               return Db::table('yc_car_info')->where('series_id',$s_id)->select();
            }
            if ($g_id){
                return Db::table('yc_car_series')->where('group_id',$g_id)->select();
            }
            if ($b_id){
                return  Db::table('yc_car_series_group')->where('brand_id',$b_id)->select();
            }
            return Db::table('yc_car_brand')->select();
        }
        public static function brands($value){
            $data['openid'] = $value['openid'];
            $data['full_name'] = $value['full_name'];
            $data['name'] = $value['name'];
            $data['displacement'] = $value['displacement'];
            $data['particular_year'] = $value['particular_year'];
            $data['vu_img'] = $value['vu_img'];
            $data['vu_time'] = time();
            $default =Db::table('yc_vehicle_user')->where('openid',$value['openid'])->where('default',1)->find();
            if ($default !=null){
                $data['default'] = 2;
            } else {
                $data['default'] = 1;
            }
            return Db::table('yc_vehicle_user')->insert($data);
        }
}
